import 'dart:async';
import 'package:flutter/services.dart';
import 'flutter_huawei_account_platform_interface.dart';

/// An implementation of [FlutterHuaweiAccountPlatform] that uses method channels.
class MethodChannelFlutterHuaweiAccount extends FlutterHuaweiAccountPlatform {
  /// The method channel used to interact with the native platform.
  final methodChannel =
      const MethodChannel('com.chyuning.flutter_huawei_account');

  @override
  Future<String?> code() async {
    final version = await methodChannel.invokeMethod<String>('code');
    return version;
  }

  @override
  PlatformViewFactory? createLoginButtonFactory() {
    return LoginButtonFactory();
  }
}

/// 登录按钮工厂实现类
class LoginButtonFactory implements PlatformViewFactory {
  @override
  PlatformViewController create(
      {int? viewId, Map<String, dynamic>? creationParams}) {
    // 在Android/OHOS平台上，PlatformView会通过PlatformViewsService进行创建
    // 这里返回一个简单的控制器实现
    return LoginButtonController(viewId!);
  }
}

/// 登录按钮控制器实现类
class LoginButtonController implements PlatformViewController {
  @override
  final int viewId;
  final MethodChannel _channel;

  LoginButtonController(this.viewId)
      : _channel = MethodChannel('com.chyuning.flutter_huawei_account/$viewId');

  @override
  Future<void> clearFocus() async {
    await _channel.invokeMethod('clearFocus');
  }

  @override
  Future<void> dispatchPointerEvent(PointerEvent event) async {
    // 不需要实现
  }

  Future<void> setOnPlatformViewCreatedCallback(
      PlatformViewCreatedCallback? callback) async {
    if (callback != null) {
      callback(viewId);
    }
  }
  
  @override
  // TODO: implement awaitingCreation
  bool get awaitingCreation => throw UnimplementedError();
  
  @override
  Future<void> create({Size? size, Offset? position}) {
    // TODO: implement create
    throw UnimplementedError();
  }
  
  @override
  Future<void> dispose() {
    // TODO: implement dispose
    throw UnimplementedError();
  }
}
